import axios from 'axios'
import type { AxiosInstance, AxiosRequestConfig, AxiosPromise } from 'axios'
import store from '@/store/index'
class Request {
  instance: AxiosInstance
  constructor() {
    this.instance = axios.create({
      baseURL: process.env.VUE_APP_BASE_URL as string
    })
    this.useRequestInterceptors()
    this.useResponseinterceptors()
  }
  public request(config: AxiosRequestConfig): AxiosPromise {
    return this.instance.request(config)
  }
  private useRequestInterceptors() {
    this.instance.interceptors.request.use((config) => {
      const token = store.state.login.token
      if (token) {
        config.headers!.Authorization = `Bearer ${token}`
      }
      return config
    })
  }
  private useResponseinterceptors() {
    this.instance.interceptors.response.use((res) => {
      return res.data
    })
  }
}
export default new Request()
